import axios from 'axios';
import { getAccessToken } from '@/utils/auth';
import mock from '@/mock'; // Import mock data

const service = axios.create({
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        // Process request data before sending the request
        if (getAccessToken()) {
            config.headers['Authorization'] = 'Bearer ' + getAccessToken(); // Attach token
        }
        return config;
    },
    error => {
        // Handle request error
        console.log(error);
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        // Process response data
        return response.data;
    },
    error => {
        // Handle response error
        console.log(error);
        return Promise.reject(error);
    }
);

// Use mock only in development environment
if (process.env.NODE_ENV !== 'production') {
    mock;
}

// Configure base API URL based on environment variables
const baseURL = process.env.ENV === 'development' ?
    `${process.env.VUE_APP_BASE_API}${process.env.VUE_APP_PATH}` :
    '/api';

// Set baseURL
service.defaults.baseURL = baseURL;

export default service;